const mongoose=require("mongoose");
const crypto =require("crypto");//加密模块

const Schema=mongoose.Schema;
const userSchame=new Schema({
    userName:{
        type:String,
        required:true,
        match:/^[A-z0-9\u4e00-\u9fa5]{2,10}$/
    },
    password:{
        type:String,
        required:true,
        match:/^[A-z0-9]{5,12}$/
    },
    sex:{
        type:String,
        enum:["男","女"],
        default:"男"
    },
    age:{
        type:Number,
        min:15,
        max:100,
        default:20
    },
    email:{
        type:String,
        match:/^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$/
    },
    phone:{
        type:Number,
        match:/^1(3|4|5|7|8)\d{9}$/
    },
    signature:{
        type:String,
        default:"这个家伙很懒，什么都没有留下"
    },
    photo:{
        type:String,
        default:"default.png"
    },
    isAdmin:{
        type:Boolean,
        default:false
    },
    date:{
        type:Date,
        default:Date.now()
    }
}) 

//加密密码
userSchame.pre('save',function(next){

    this.password=crypto.createHash('sha256').update(this.password).digest("hex");

    next();
})


module.exports=mongoose.model("user",userSchame);